/**
 * 培训通知列表
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="zt">
							<template #label><vp-label zh="状态" en="Status" /></template>
							<el-select v-model="searchForm.zt" clearable>
								<el-option :value="10" label="待开课"></el-option>
                            	<el-option :value="20" label="开课中"></el-option>
								<el-option :value="30" label="已结束"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="sjKsrq">
							<template #label><vp-label zh="培训时间" en="Training Time"/></template>
							<el-date-picker
								v-model="searchForm.sjKsrq"
								type="daterange"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
								clearable
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="kc">
							<template #label><vp-label zh="课程" en="Course"/></template>
							<el-input v-model="searchForm.kc" v-trim clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="pxbh">
							<template #label><vp-label zh="培训编号" en="Training No." /></template>
							<el-input v-model="searchForm.pxbh" v-trim clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event" />
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #table>
			<vp-vxe-table
				id="trainingNoticeTable"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column 
					field="pxbh" 
					title="培训编号 Training No."
					show-overflow 
					width="120"
				>
					<template #header><vp-label zh="培训编号" en="Training No." /></template>
					<template #default="{ row }">
						<el-link @click="handleViewTrainingPlan(row.id)">{{ row.pxbh }}</el-link>
					</template>
				</vxe-column>
				<vxe-column 
					field="pxzt" 
					title="状态 Status"
					show-overflow 
					width="80"
				>
					<template #header><vp-label zh="状态" en="Status" /></template>
					<template #default="{ row }">
						<span>{{ row.pxzt === 10 ? '待开课' : row.pxzt === 20 ? '开课中' : row.pxzt === 30 ? '已结束' : '' }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="kcbh" 
					title="课程编号 Course No."
					show-overflow
					width="120"
				>
					<template #header><vp-label zh="课程编号" en="Course No."/></template>
					<template #default="{ row }">
						<el-link @click="handleViewCourse(row.kcid)">{{ row.kcbh }}</el-link>
					</template>
				</vxe-column>
				<vxe-column 
					field="kcmc_zw" 
					title="课程名称 Course Name"
					show-overflow 
					min-width="200"
				>
					<template #header><vp-label zh="课程名称" en="Course Name" /></template>
					<template #default="{ row }">
						<span>{{ (row.kcmcZw || '') + ' ' + (row.kcmcYw || '') }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sj_ksrq" 
					title="培训时间 Training Time"
					show-overflow
					width="270"
				>
					<template #header><vp-label zh="培训时间" en="Training Time" /></template>
					<template #default="{ row }">
						<span>{{ row.sjKsrq + (row.sjKssj && row.sjKssj !== '00:00' ? ' ' + row.sjKssj : '') }}</span>
						<span v-if="row.sjJsrq && row.sjKsrq !== row.sjJsrq && row.sjKssj !== row.sjJssj"> 
							~ {{ row.sjJsrq + (row.sjJssj && row.sjJssj !== '00:00' ? ' ' + row.sjJssj : '') }}
						</span>
					</template>
				</vxe-column>
				<vxe-column 
					field="zxxxbs" 
					title="需学习 Need to Learn"
					show-overflow 
					width="110"
					align="center"
				>
					<template #header><vp-label zh="需学习" en="Need to Learn" /></template>
					<template #default="{ row }">
						<el-tag v-if="row.zxxxbs === 10" type="primary">是 YES</el-tag>
						<el-tag v-else type="info">否 NO</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					field="sczxxxbs" 
					title="已学习 Learned"
					show-overflow 
					width="110"
					align="center"
				>
					<template #header><vp-label zh="已学习" en="Learned" /></template>
					<template #default="{ row }">
						<el-tag v-if="row.sczxxxbs === 30" type="primary">是 YES</el-tag>
						<el-tag v-else type="info">否 NO</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					field="zxksbs" 
					title="需考试 Need to Exam"
					show-overflow 
					width="110"
					align="center"
				>
					<template #header><vp-label zh="需考试" en="Need to Exam" /></template>
					<template #default="{ row }">
						<el-tag v-if="row.zxksbs === 10" type="primary">是 YES</el-tag>
						<el-tag v-else type="info">否 NO</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					field="sczxksbs" 
					title="已考试 Examined"
					show-overflow 
					width="110"
					align="center"
				>
					<template #header><vp-label zh="已考试" en="Examined" /></template>
					<template #default="{ row }">
						<el-tag v-if="row.sczxksbs === 30" type="primary">是 YES</el-tag>
						<el-tag v-else type="info">否 NO</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					field="pxdd" 
					title="培训地点 Location"
					show-overflow 
					width="120"
				>
					<template #header><vp-label zh="培训地点" en="Location" /></template>
				</vxe-column>
				<vxe-column 
					field="dabh" 
					title="教员 Trainer"
					show-overflow 
					width="140"
				>
					<template #header><vp-label zh="教员" en="Trainer" /></template>
					<template #default="{ row }">
						<el-link @click="handleViewPersonnelFile(row.jydaid)">{{ row.jydabh + ' ' + row.jyxm }}</el-link>
					</template>
				</vxe-column>
				<vxe-column 
					field="operation" 
					title="操作 Operation"
					fixed="right" 
					width="140"
				>
					<template #header><vp-label-operations/></template>
					<template #default="{ row }">
						<el-space>
							<vp-btn v-if="row.pxzt === 20 && row.zxxxbs === 10 && row.rylx === 1" type="primary" text @click="handleOnlineLearning(row)">
								<vp-label zh="学习" en="Learning" />
							</vp-btn>
							<vp-btn v-if="row.pxzt === 20 && row.zxksbs === 10 && row.rylx === 1 && [30,99].includes(row.sczxxxbs) && !row.jjsj" type="primary" text @click="handleOnlineExam(row)">
								<vp-label zh="考试" en="Exam" />
							</vp-btn>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<VpOnlineLearning ref="vpOnlineLearningRef" @close="loadDataList"></VpOnlineLearning>
		<VpOnlineExam ref="vpOnlineExamRef" @close="loadDataList"></VpOnlineExam>
	</vp-main-page>
</template>
<script setup lang="ts" name="trainingNotice">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue'
import request from '/@/utils/request';
import { useRouter } from 'vue-router';
import { DEFAULT_PAGE } from '/@/utils/page';

const VpOnlineLearning = defineAsyncComponent(() => import('/@/components/vp/learning/VpOnlineLearning.vue'));
const VpOnlineExam = defineAsyncComponent(() => import('/@/components/vp/learning/VpOnlineExam.vue'));

const searchForm = reactive({
	zt: 20,
	sjKsrq: [],
	kc: "",
	pxbh: "",
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

const router = useRouter();

// 加载列表数据
const loadDataList = () => {
	loading.value = true;
	request.get('/training/notice/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	loadDataList();
})

const tableRef = ref();
const editTrainingRecordRef = ref();

// 新增
const handleAdd = ()=>{
	editTrainingRecordRef.value.handleOpen({
		type: 'add',
	});
}

// 修改
const handleEdit = (row: any)=>{
	editTrainingRecordRef.value.handleOpen({
		type: 'edit',
		id: row.id
	});
}

// 查看培训计划
const handleViewTrainingPlan = (id: string)=>{
	router.push({
		name: "viewTrainingPlan",
		params: {
			id: id,
		},
	})
}

// 查看课程
const handleViewCourse = (id: string)=>{
	router.push({
		name: "viewTrainingCourse",
		params: {
			id: id,
		},
	})
}

// 查看人员档案
const handleViewPersonnelFile = (id: string) => {
	router.push({
		name: "viewTrainingPersonnelFile",
		params: {
			id: id,
		},
	})
}

// 在线学习
const vpOnlineLearningRef = ref();
const handleOnlineLearning = (row: any) => {
	vpOnlineLearningRef.value.handleOpen({
		courseId: row.kcid,	// 课程id
		trainingRecordId: row.scid	// 培训记录id（实参id）
	});
}

// 在线考试
const vpOnlineExamRef = ref();
const handleOnlineExam = (row: any) => {
	vpOnlineExamRef.value.handleOpen({
		id: row.id,	// 培训计划id
	});
}

</script>

<style scoped lang="scss">
</style>